﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizzCore;
using BizzCommInterfaces;
using BizzUI.Forms;

namespace BizzUI.UserControls
{
    

    public partial class RuleControl : UserControl
    {
        public RuleControl()
        {
            InitializeComponent();
            
            initRules();
        }

        

        private void initRules()
        {
            BizzSystem s = BizzSystem.getInstance();
            
         
            List<RuleView> rules = new List<RuleView>();
            foreach (BizzCore.Rule rule in s.getRuleList())
            {
                rules.Add(new RuleView()
                {
                    rule = rule,
                    _name = rule.ruleName,
                    _field = rule.ruleField,
                    _condition = rule._condition.ToString(),
                    _expectedValue = rule.ruleExpectedValue,
                    _outbox = rule._outbox.ToString()
                });
            }
            dgRules.DataSource = rules;
            foreach (DataGridViewColumn c in dgRules.Columns)
            {
                c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }
            dgRules.Columns["rule"].Visible = false;
            dgRules.Columns["_outbox"].HeaderText = "Bandeja de salida";
            dgRules.Columns["_outbox"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgRules.Columns["_name"].HeaderText = "Nombre";
            dgRules.Columns["_field"].HeaderText = "Campo";
            dgRules.Columns["_condition"].HeaderText = "Condición";
            dgRules.Columns["_expectedValue"].HeaderText = "Valor esperado";
            dgRules.Columns["_condition"].DefaultCellStyle.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold);
            
            dgRules.RowHeadersVisible = false;


        }

        public List<BizzCore.Rule> getSelectedRules()
        {
            List<BizzCore.Rule> rules = new List<BizzCore.Rule>();
            foreach (DataGridViewRow dgRule in dgRules.SelectedRows)
            {
                BizzCore.Rule rule = (BizzCore.Rule)dgRule.Cells[0].Value;
                rules.Add(rule);
            }
            return rules;
        }

        class RuleView
        {
            public BizzCore.Rule rule { get; set; }
            public string _name { get; set; }
            public string _field { get; set; }
            public string _condition { get; set; }
            public string _expectedValue { get; set; }
            
            public string _outbox { get; set; }
            
        }

        private void tsNueva_Click(object sender, EventArgs e)
        {
            NewRuleForm newRule = new NewRuleForm();
            newRule.ShowDialog();
            initRules();
        }

        private void tsEliminar_Click(object sender, EventArgs e)
        {
            foreach (BizzCore.Rule theRule in this.getSelectedRules())
            {
                BizzSystem.getInstance().removeRule(theRule);
            }
            initRules();
        }
        
    }
}
